<?php 
class ControllerModuleCart extends Controller { 
	protected function index() {
		$this->language->load('module/cart');
		
		$this->load->model('tool/seo_url');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
    	
		$this->data['text_subtotal'] = $this->language->get('text_subtotal');
		$this->data['text_empty'] = $this->language->get('text_empty');
		$this->data['text_remove'] = $this->language->get('text_remove');
		$this->data['text_confirm'] = $this->language->get('text_confirm');
		$this->data['text_view'] = $this->language->get('text_view');
		$this->data['text_checkout'] = $this->language->get('text_checkout');

        $this->data['template'] = $this->config->get('config_template');

		$this->data['view'] = HTTP_SERVER . 'index.php?route=checkout/cart';
		$this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/cart';
        
		$giftcard_total = 0;
		$giftcard_items = 0;
		if (isset($this->session->data['giftcards']) && $this->session->data['giftcards']) {
			foreach ($this->session->data['giftcards'] as $key => $giftcard) {
				$giftcard_total += $giftcard['amount'];
			}
			
			$giftcard_items = count($this->session->data['giftcards']);
		}
		
		$this->data['subtotals'] = $this->currency->formatPrice($this->cart->getSubTotal() + $giftcard_total);
        $this->data['count_products'] = $this->cart->countProducts() + $giftcard_items; 
        
		/*
		$this->data['products'] = array();
		
    	foreach ($this->cart->getProducts() as $result) {
        	$option_data = array();

        	foreach ($result['option'] as $option) {
          		$option_data[] = array(
            		'name'  => $option['name'],
            		'value' => $option['value']
          		);
        	}
			
      		$this->data['products'][] = array(
        		'key' 		 => $result['key'],
        		'name'       => $result['name'],
				'option'     => $option_data,
        		'quantity'   => $result['quantity'],
				'stock'      => $result['stock'],
				'price'      => $this->currency->formatPrice($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
				'href'       => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']),
      		);
    	}
		
		$this->data['giftcards'] = array();		
		if (isset($this->session->data['giftcards']) && $this->session->data['giftcards']) {
			foreach ($this->session->data['giftcards'] as $key => $giftcard) {
				$this->data['giftcards'][] = array(
					'key'         => $key,
					'description' => $giftcard['description'],
					'amount'      => $this->currency->format($giftcard['amount'])
				);
			}
		}
		
		if (!$this->config->get('config_customer_price')) {
			$this->data['display_price'] = TRUE;
		} elseif ($this->customer->isLogged()) {
			$this->data['display_price'] = TRUE;
		} else {
			$this->data['display_price'] = FALSE;
		}
	
		$total_data = array();
		$total = 0;
		$taxes = $this->cart->getTaxes();
		 
		$this->load->model('checkout/extension');
		
		$sort_order = array(); 
		
		$results = $this->model_checkout_extension->getExtensions('total');
		
		foreach ($results as $key => $value) {
			$sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
		}
		
		array_multisort($sort_order, SORT_ASC, $results);
		
		foreach ($results as $result) {
			$this->load->model('total/' . $result['key']);

			$this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
		}
		
		$sort_order = array(); 
	  
		foreach ($total_data as $key => $value) {
      		$sort_order[$key] = $value['sort_order'];
    	}

    	array_multisort($sort_order, SORT_ASC, $total_data);
		
    	$this->data['totals'] = $total_data;
		
		$this->data['ajax'] = $this->config->get('cart_ajax');
		*/
		
		$this->id = 'cart';
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cart.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/cart.tpl';
		} else {
			$this->template = 'default/template/module/cart.tpl';
		}
		
		$this->render();
	}
		
	public function callback() {
		$this->language->load('module/cart');

		$this->load->model('tool/seo_url');
		
		unset($this->session->data['shipping_methods']);
		unset($this->session->data['shipping_method']);
		unset($this->session->data['payment_methods']);
		unset($this->session->data['payment_method']);	
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			
			if (isset($this->request->post['remove'])) {
	    		$result = explode('_', $this->request->post['remove']);
          		$this->cart->remove(trim($result[1]));
      		} else {
				if (isset($this->request->post['option'])) {
					$option = $this->request->post['option'];
				} else {
					$option = array();	
				}
				
      			$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option);
			}
		}
				
		$output = '<table cellpadding="2" cellspacing="0" style="width: 100%;">';
		
		if ($this->cart->getProducts()) {
		
    		foreach ($this->cart->getProducts() as $product) {
      			$output .= '<tr>';
        		$output .= '<td width="1" valign="top" align="left"><span class="cart_remove" id="remove_ ' . $product['key'] . '" />&nbsp;</span></td><td width="1" valign="top" align="right">' . $product['quantity'] . '&nbsp;x&nbsp;</td>';
        		$output .= '<td align="left" valign="top"><a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $product['product_id']) . '">' . $product['name'] . '</a>';
          		$output .= '<div>';
	            
				foreach ($product['option'] as $option) {
            		$output .= ' - <small style="color: #999;">' . $option['name'] . ' ' . $option['value'] . '</small><br />';
	            }
				
				$output .= '</div></td>';
				$output .= '</tr>';
      		}
			
			$output .= '</table>';
    		$output .= '<br />';
    		
    		$total = 0;
			$taxes = $this->cart->getTaxes();
			 
			$this->load->model('checkout/extension');
			
			$sort_order = array(); 
			
			$view = HTTP_SERVER . 'index.php?route=checkout/cart';
			$checkout = HTTPS_SERVER . 'index.php?route=checkout/shipping';
			
			$results = $this->model_checkout_extension->getExtensions('total');
			
			foreach ($results as $key => $value) {
				$sort_order[$key] = $this->config->get($value['key'] . '_sort_order');
			}
			
			array_multisort($sort_order, SORT_ASC, $results);
			
			foreach ($results as $result) {
				$this->load->model('total/' . $result['key']);

				$this->{'model_total_' . $result['key']}->getTotal($total_data, $total, $taxes);
			}
			
			$sort_order = array(); 
		  
			foreach ($total_data as $key => $value) {
      			$sort_order[$key] = $value['sort_order'];
    		}

    		array_multisort($sort_order, SORT_ASC, $total_data);
    	    		
    		$output .= '<table cellpadding="0" cellspacing="0" align="right" style="display:inline-block;">';
      		foreach ($total_data as $total) {
      			$output .= '<tr>';
		        $output .= '<td align="right"><span class="cart_module_total"><b>' . $total['title'] . '</b></span></td>';
		        $output .= '<td align="right"><span class="cart_module_total">' . $total['text'] . '</span></td>';
      			$output .= '</tr>';
      		}
      		$output .= '</table>';
      		$output .= '<div style="padding-top:5px;text-align:center;clear:both;"><a href="' . $view . '">' . $this->language->get('text_view') . '</a> | <a href="' . $checkout . '">' . $this->language->get('text_checkout') . '</a></div>';
		} else {
			$output .= '<div style="text-align: center;">' . $this->language->get('text_empty') . '</div>';
		}
		
		$this->response->setOutput($output, $this->config->get('config_compression'));
	}
    
    public function refreshCart() {
        $this->language->load('module/cart');
          
        $this->load->model('tool/seo_url');        
          
        unset($this->session->data['shipping_methods']);
        unset($this->session->data['shipping_method']);
        unset($this->session->data['payment_methods']);
        unset($this->session->data['payment_method']);    
         
        if ($this->request->server['REQUEST_METHOD'] == 'POST'){            
            if (isset($this->request->post['remove'])) {
                $result = explode('_', $this->request->post['remove']);
                $this->cart->remove(trim($result[1]));
            }
            else {
                if (isset($this->request->post['option'])) {
                    $option = $this->request->post['option'];
                } 
                else {
                    $option = array();    
                }
                
                if(isset($this->request->post['quantity'])){
                    $quantity = $this->request->post['quantity'];
                    $qty = $this->request->post['quantity'];
                }
                else{
                    $quantity = 0;
                    $qty = null;    
                }
                    
                if($this->cart->isProductInStock($this->request->post['product_id'], $option, $quantity)){                   
                    $this->cart->add($this->request->post['product_id'], $qty, $option);
                }                
            }
        }

        /*
        $checkout = HTTPS_SERVER . 'index.php?route=checkout/cart';
        $output = "";       
         
        if ($this->cart->getProducts()) {       
            $total_items = $this->cart->countProducts();        
            $subtotals = $this->currency->formatPrice($this->cart->getSubTotal());
            $output .= '<a href="#" class="txt-vare" >'.$total_items.' STK = '.$subtotals.'</a><span class="arrow-down show-cart-list"></span>';
        } 
        else {
            $output = '<a href="#" class="txt-vare" >0 STK = 0 </a><span class="arrow-down show-cart-list"></span>';
        }
        
        $this->response->setOutput($output, $this->config->get('config_compression'));
        */

        $this->index();
        $this->response->setOutput($this->output, $this->config->get('config_compression'));
    }
    
    public function getCart(){
    	$this->language->load('module/cart');
    	if ($this->cart->getProducts()) {
    		
    		$output = '<table class="viewshoppingbag" width="100%" cellspacing="0" cellpadding="0">
                                  <tr class="HeadTb"> 
                                        <td class="proImg"><span> Varebeskrivelse</span></td>
                                        <td class="proName"><span>&nbsp; </span></td>
                                        <td class="proFave"><span>Farve</span></td>
                                        <td class="proSize"><span>Størrelse</span>  </td> 
                                        <td class="proNumber"> <span>Antal</span> </td>    
                                        <td class="proPrice"> <span>Pris pr. stk</span> </td>
                                        <td class="proPriceTT"><span>Pris i alt</span></td>
                                        <td class="proDel"><span>&nbsp;</span></td>
                                    </tr><!--eachRowPro--> 
                                    <tr class="space">
                                            <td colspan="8"></td> 
                                    </tr><!--eachRowPro-->'; 
                                    
                                    
                              //</table> ';    		
    		
    		foreach ($this->cart->getProducts() as $product) {
    			$output .= '<tr class="eachRowPro">
                                        <td class="proImg"><div class="boxThumb"><img src="imgProd/thumbs/triumph_thumb1.jpg" alt="" /></div></td>
                                        <td class="proName"><h2 class="proName"><a href="#">'.$product['name'].'</a></h2><p>Vare-nummer: 30283</p></td>
                                        <td class="proFave"><span>Hvid</span></td>
                                        <td class="proSize"><span>S</span>  </td> 
                                        <td class="proNumber"> <div class="wrapedit"> <input class="inputNumber" value="1"> <div class="editNum"><span class="add">+</span><span class="sub">-</span></div>  </div> </td>    
                                        <td class="proPrice"> <span>690 DKK </span> </td>
                                        <td class="proPriceTT" ><span>690 DKK </span></td>
                                        <td class="proDel" ><span>  <img src="images/btnDel.jpg" alt="Delete"> </span></td>
                                    </tr><!--eachRowPro--> ';
    		}
    		
    		$output .= '</table> ';
    	}
    }
}
?>